package com.zm.zmgz.service;

import com.zm.zmgz.dao.RevenueStatisticsBeanMapper;
import com.zm.zmgz.pojo.RevenueBean;
import com.zm.zmgz.pojo.RevenueStatisticsBean;
import com.zm.zmgz.pojo.RevenueStatisticsBeanExample;
import com.zm.zmgz.utils.DateUtils;
import com.zm.zmgz.utils.StringCjwUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: cjw
 * @Date: 2020/06/02/18:35
 * @Description:收支统计表业务层
 */
@Service
public class RevenueStatisticsService {

    @Autowired
    RevenueStatisticsBeanMapper mapper;

    @Autowired
    BusinessAnalysisService businessAnalysisService; // 营业分析表业务层

    @Autowired
    RevenueService revenueService; // 收支明细表业务层

    /***
     * @Description: 根据批次id删除该批次记录
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-06-02 18:36:55
     */
    public Integer delteRevenueStatisticsByBatch(Integer batchId) {
        if (Objects.isNull(batchId))
            return null;
        RevenueStatisticsBeanExample example = new RevenueStatisticsBeanExample();
        RevenueStatisticsBeanExample.Criteria criteria = example.createCriteria();
        criteria.andBatchidEqualTo(batchId);
        return mapper.deleteByExample(example);

    }


    /***
     * @Description: 遍历收支明细表生成记录(需要统计, 以收支类别和类别进行统计)
     * 通过批次id获取批次日期
     * 由于是按照批次和类别进行统计的,就需要知道该批次的种类集合
     * 通过批次获取种类集合
     * 遍历种类集合
     *  通过批次和种类,获取记录集合,统计记录集合生成记录.
     *
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-06-02 18:41:55
     * 19点21分 - 21点22分
     */
    public Integer addRevenueStatisticsByBatchId(Integer batchId) {
        // 通过批次id获取批次日期
        String batch = businessAnalysisService.getBatchById(batchId);
        if (Objects.isNull(batch))
            return null;
        Date date = DateUtils.getDateByString(batch, "yyyy年MM月");
        // 通过批次获取种类集合
        List<String> list = revenueService.getSourceByDate(date);
        if (Objects.isNull(list) || list.size() == 0)
            return null;
        // 遍历种类集合;通过批次,种类查询收支记录集合,一个集合生成一条收支统计记录
        int i = 0;
        for (String s : list) {
            List<RevenueBean> list1 = revenueService.getRevenuesByDateAndSource(date, s);
            if (Objects.isNull(list1) || list1.size() == 0)
                continue;
            RevenueBean bean1 = list1.get(0);
            // 创建收支统计对象;设置批次id,收支种类,种类
            RevenueStatisticsBean bean = new RevenueStatisticsBean();
            bean.setBatchid(batchId);
            bean.setRevenuekind(bean1.getRevenue());
            bean.setKind(bean1.getSource());
            // 遍历收支明细集合,累加数额并设置
            Double v = new Double(0);
            for (RevenueBean revenueBean : list1) {
                String amount = revenueBean.getAmount();
                double v1 = StringCjwUtils.getDoubleByString(amount);
                v += v1;
            }
            bean.setAmount(new BigDecimal(v));
            i += addNewRevenueStatistics(bean);
        }
        return i;
    }

    /***
     * @Description: 添加一个新对象到数据库中
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-06-02 21:06:38
     */
    public Integer addNewRevenueStatistics(RevenueStatisticsBean bean) {
        if (Objects.isNull(bean))
            return null;
        int i = mapper.insert(bean);
        return i;
    }


    /***
     * @Description: 通过批次id和收支种类获取收支记录集合
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-06-02 21:49:57
     */
    public List<RevenueStatisticsBean> getRevenueStatisticsByBatchIdAndRevenueKind(Integer batchId,String revenueKind) {
        if(Objects.isNull(batchId))
            return null;
        RevenueStatisticsBeanExample example = new RevenueStatisticsBeanExample();
        RevenueStatisticsBeanExample.Criteria criteria = example.createCriteria();
        criteria.andBatchidEqualTo(batchId).andRevenuekindEqualTo(revenueKind);
        List<RevenueStatisticsBean> beanList = mapper.selectByExample(example);
        if(Objects.isNull(beanList) || beanList.size() == 0)
            return null;
        else
            return beanList;
    }


    /***
    * @Description:累加传入集合中的amount字段并返回
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-06-03 08:41:30
    */
    public BigDecimal getTotalAmountByRevenueStatisList(List<RevenueStatisticsBean> beanList) {
        if (Objects.isNull(beanList) || beanList.size() == 0)
            return null;
        BigDecimal decimal = new BigDecimal(0);
        for (RevenueStatisticsBean bean : beanList) {
            BigDecimal amount = bean.getAmount();
            decimal = decimal.add(amount);
        }
        return decimal;
    }
}
